<template>
	<view class="container">
		<view class="title-sub-1">选择 2.4GHz Wi-Fi网络并输入密码</view>
		<view class="title-sub-2">如果你的 Wi-Fi是 5GHz的，请先设置为 2.4GHZ</view>
		<view class="setting-wrapper">
			<view class="setting-title">网络设置方法</view>
			<view class="setting-content">跳转到系统Wi-Fi的设置列表>选择2.4GHz且名称为非中文的Wi-Fi进行连接>连接后返回小程序网络配置界面</view>
		</view>
		<image v-if="wifiList.length == 0 && !isAndroid" class="wifi-icon" src="@/static/ic_wifi_list.png"></image>
		<template v-else>
			<scroll-view scroll-y class="wifi-list-scroll">
				<view class="wifi-list">
					<view class="wifi-item" v-for="(wifi, index) in wifiList" :key="index" @click="onWifi(wifi, index)">
						<view class="wifi-name">{{ wifi.SSID || '未知网络' }}</view>
						<image v-if="wifi.selected" class="wifi-btn" :src="$IMAGE_BASE_URL + '/static/ic_selected.png'">
						</image>
						<image v-else class="wifi-btn" :src="$IMAGE_BASE_URL + '/static/ic_unselected.png'"></image>
					</view>
				</view>
			</scroll-view>
		</template>
		<view class="btn-wrapper">
			<BaseButton v-if="wifiList.length == 0 && !isAndroid" shape="circle" text="去设置" @click="toWifiConfig" />
			<BaseButton v-else shape="circle" text="确定" :disabled="!selectedWifi" @click="onConfirm" />
		</view>
	</view>
</template>

<script>
	import BaseButton from "@/components/base-button/index.vue";
	export default {
		data() {
			return {
				wifiList: [],
				selectedWifi: undefined,
				isAndroid: true,
			};
		},
		components: {
			BaseButton
		},
		onLoad() {
			this.initWifi();
			const systemInfo = uni.getSystemInfoSync();
			this.isAndroid = (systemInfo.osName == "android");
		},
		methods: {
			initWifi() {
				uni.onGetWifiList((res) => {
					console.log("onGetWifiList wifi list:", res);
					if (!!res && !!res.wifiList) {
						this.wifiList = res.wifiList.map(v => {
							return {
								...v,
								selected: false,
							}
						});
						this.wifiList = this.wifiList.filter(v => !!v.SSID);
					}
				})
				uni.startWifi({
					success: (res) => {
						console.log("startWifi success", res);
					},
					fail: (err) => {
						console.log("startWifi err", err);
					}
				})
			},
			getWifiList() {
				uni.getWifiList({
					success: (res) => {
						console.log("getWifiList success", res);
					},
					fail: (err) => {
						console.log("getWifiList error", err);
					}
				})
			},
			onWifi(wifi, index) {
				for (let i = 0; i < this.wifiList.length; i++) {
					this.wifiList[i].selected = (index == i);
				}
				this.selectedWifi = wifi;
			},
			toWifiConfig() {
				this.getWifiList();
			},
			onConfirm() {
				console.log("wifi", this.selectedWifi);
				const pages = getCurrentPages();
				const prevPage = pages[pages.length - 2];
				prevPage.$vm.onWifiChange({
					...this.selectedWifi,
				});
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #F1F2F4;
	}

	.container {

		.title-sub-1 {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #fa7601;
			text-align: center;
			margin-top: 53rpx;
		}

		.title-sub-2 {
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #6C6C6C;
			text-align: center;
			margin: 20rpx 0 30rpx 0;
		}

		.setting-wrapper {
			width: 678rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 30rpx;
			box-sizing: border-box;
			margin: 0 auto;

			.setting-title {
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 26rpx;
				color: #3E3737;
				text-align: left;
				margin-bottom: 10rpx;
			}

			.setting-content {
				font-family: PingFang-SC, PingFang-SC;
				font-size: 26rpx;
				color: #6C6C6C;
				text-align: left;
				line-height: 50rpx;
			}
		}

		.wifi-icon {
			display: block;
			width: 554rpx;
			height: 595rpx;
			margin: 74rpx auto 0 auto;
		}

		.wifi-list-scroll {
			width: 678rpx;
			height: 750rpx;
			background-color: #FFFFFF;
			margin: 20rpx auto 0 auto;
			border-radius: 20rpx;

			.wifi-list {
				width: 100%;

				.wifi-item {
					height: 115rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 1rpx #D8D8D8 solid;
					padding: 0 30rpx;

					.wifi-name {
						font-size: 30rpx;
						color: #010101;
					}

					.wifi-btn {
						width: 35rpx;
						height: 35rpx;
					}
				}

				.wifi-item:last-child {
					border-bottom-width: 0;
				}
			}
		}

		.btn-wrapper {
			position: absolute;
			bottom: 90rpx;
			left: 50%;
			transform: translateX(-50%);
		}
	}
</style>